package com.web.base;

import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
import com.web.bean.LogDuoDing;

/**
 * 一个简单的Baseaction <br/>
 * 用来处理简单的添加删除修改.<br/>
 * 
 * @author Administrator
 */
 
public class BaseAction extends ActionSupport {

	protected final Logger logger = Logger.getLogger(getClass());
	protected BaseService service;
	/**
	 * 错误的消息
	 */
	protected String commondResult;
	// 返回当前要返回的所有的数据
	protected List<Serializable> autolist;
	/**
	 * 可以从request中获取,传过来的值.<br/>
	 * 当前要显示的页数
	 */
	protected int nowpage;
	/**
	 * 一页显示的数据行数
	 */
	protected int pagesize;
	/**
	 * 用来临时保存id的.<br/>
	 */
	protected int id;
	/**
	 * 读取列表,分页的时候用来传递hql
	 */
	protected String hql;

	// 操作的状态
	/***************************************************************************
	 * 1、 2、 3、 4、 5、 操作的状态<br/>
	 * 1 保存,修改<br/>
	 * 2 返回所有的记录<br/>
	 * 3 查找 分页<br/>
	 * 4 读取单挑记录<br/>
	 * 5 ,6 重复都是读取单条记录<br/>
	 * 其他 空方法,返回到session<br/>
	 **************************************************************************/
	private int act;
	/**
	 * 分页的对象<br/>
	 * 再分页得时候返回这个对象,用来传值
	 */
	protected Page page;

	/**
	 * 页面对象，用来传值的
	 * 
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	protected Serializable objectForm;

	private boolean savelog = true;

	/**
	 * 执行对应的操作,添加.删除.修改.显示.分页等等 默认调用的方法，这里的方法主要为url重写服务。
	 * 
	 * @return
	 */
	public String action() {
		
System.out.println(this.act+"*********************");
		if (act == 1) {
			// 读取所有的记录
			return "add" + save();
		}else if (act == 2) {
			// 读取所有的记录
			autolist = findByHql(getHql());
			return "list" + SUCCESS;
		} else if (act == 3) {
			// 按照hql读取数据（分页）
			page = findPage(getHql(), getNowpage(), getPagesize());
			return "page" + SUCCESS;
		} else if (act == 4) {
			// 用于复制
			objectForm = getService().findObjectById(objectForm, id);
			logger.info(objectForm);
			return "read" + SUCCESS;
		} else if (act == 5) {
			// 用于编辑
			objectForm = getService().findObjectById(objectForm, id);
			logger.info(objectForm);
			return "read" + SUCCESS;
		} else if (act == 6) {
			// 用于展示
			objectForm = getService().findObjectById(objectForm, id);
			logger.info(objectForm);
			return "read" + SUCCESS;
		} else if (act == 7) {
			// 用于删除
			getService().deleteById(objectForm, id);
			logger.info(objectForm);
			return "del" + SUCCESS;
		} else {
			return SUCCESS;
		}
	}

	/**
	 * 默认值执行这个方法<br/>
	 * 这里自动调用action
	 * 
	 * @return
	 * @throws java.lang.Exception
	 */
	public String execute() throws Exception {
		String opInfo = action();
		if (commondResult != null) {
		}
		return opInfo;
	}
	
	public String index(){
		return SUCCESS;
	}

	/**
	 * 保存&修改的方法
	 * 
	 * @return
	 */
	public String save() {
		try {
			service.saveOrUpdate(objectForm);
			savelog("save the objectForm . the object" + " value is :"
					+ objectForm);
			return "success";
		} catch (Exception ex) {
			return ERROR;
		}
	}

	/**
	 * 通过主键 id 来读取对象
	 * 
	 * @param id
	 * @return
	 */
	public String readoneById(int id) {
		try {
			objectForm = service.findObjectById(objectForm, id);
			return SUCCESS;
		} catch (Exception ex) {
			return ERROR;
		}
	}

	/**
	 * 通过主键id来读取数据
	 * 
	 * @param id
	 * @return
	 */
	public String readoneById(Object id) {
		try {
			objectForm = service.findObjectById(objectForm, (String) id);
			return SUCCESS;
		} catch (Exception ex) {
			return ERROR;
		}
	}

	/**
	 * 删除id对应的对象
	 * 
	 * @param id
	 * @return
	 */
	public String delById(int id) {
		if (objectForm != null) {
			service.deleteById(objectForm, id);
			savelog("del the othere object ,the object id is :" + id);
			return "success";
		} else {
			return ERROR;
		}
	}

	/**
	 * 删除删除一组数据
	 * 
	 * @param id
	 *            “123,123,123”
	 * @return
	 */
	public String delByIds(String id) {
		if (objectForm != null) {
			service.deleteById(objectForm, id);
			savelog("del the othere object ,the object id is :" + id);
			return "success";
		} else {
			return ERROR;
		}
	}

	/**
	 * 查找hql对应的数据
	 * 
	 * @param hql
	 * @return
	 */
	public List<Serializable> findByHql(String hql) {
		return service.findByHql(hql);
	}

	/**
	 * 查找hql 当前页的数据
	 * 
	 * @param hql
	 * @param nowpage1
	 * @param pagesize
	 * @return 对象的列表
	 */
	public List findPageList(String hql, int nowpage1, int pagesize) {

		if ("".equals(hql)) {
			return null;
		} else {
			if (nowpage1 >= 1) {
				return service.findByHql(nowpage1, pagesize, hql);
			} else {
				return findByHql(hql);
			}
		}

	}

	/**
	 * 通过hql读取分页，返回page对象
	 * 
	 * @param hql
	 * @param nowpage
	 * @param pagesize
	 * @return
	 */
	public Page findPage(String hql, int nowpage, int pagesize) {

		Page _Page = new Page(nowpage, service.countByHql(" select count( t ) "
				+ hql), pagesize, findPageList(hql, nowpage, pagesize));

		logger.info("findPage is work  hql=：" + hql);
		return _Page;

	}

	/**
	 * 保存日志，如果不需要可以设定
	 * 
	 * @param dowhat
	 */

	public void savelog(String dowhat) {
		if (isSavelog()) {
			LogDuoDing _Log = new LogDuoDing();
			_Log.setLogContent(dowhat);
			service.save(_Log);
		}
	}

	public BaseService getService() {
		return service;
	}

	public void setService(BaseService service) {
		this.service = service;
	}

	public Logger getLogger() {
		return logger;
	}

	public void setNowpage(int nowpage) {
		this.nowpage = nowpage;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<Serializable> getAutolist() {
		return autolist;
	}

	public void setAutolist(List<Serializable> autolist) {
		this.autolist = autolist;
	}

	public int getPagesize() {

		if (pagesize < 1) {
			// 默认15条
			pagesize = 15;
		}

		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getAct() {
		return act;
	}

	public void setAct(int act) {
		this.act = act;
	}

	public String getHql() {
		if ("".equals(hql) || hql == "" || hql == null) {
			logger.info("do this");
			hql = "  from " + objectForm.getClass().getName()
					+ " t order by t.id desc";
		}
		return hql;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	/**
	 * 获取当前分页
	 * 
	 * @return
	 */
	public int getNowpage() {
		if (nowpage < 1) {
			nowpage = 1;
		}
		return nowpage;
	}

	/**
	 * 获取当前的错误消息
	 * 
	 * @return
	 */
	public String getCommondResult() {
		return commondResult;
	}

	/**
	 * 设置错误消息
	 * 
	 * @param commondResult
	 */
	public void setCommondResult(String commondResult) {
		this.commondResult = commondResult;
	}

	/**
	 * 页面上要用这个名字来命名form中的各个input<br/>
	 * struts2会自动封装到对象里面
	 * 
	 * @return
	 */
	public Serializable getObjectForm() {
		return objectForm;
	}

	/**
	 * 设置objectForm
	 * 
	 * @param objectForm
	 */
	public void setObjectForm(Serializable objectForm) {
		this.objectForm = objectForm;
	}

	/***************************************************************************
	 * 
	 * @return
	 **************************************************************************/
	public Serializable getEtaoobject() {
		return objectForm;
	}

	public void setEtaoobject(Serializable objectForm) {
		this.objectForm = objectForm;
	}

	public boolean isSavelog() {
		return savelog;
	}

	public void setSavelog(boolean savelog) {
		this.savelog = savelog;
	}

}
